﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using TianlalaHSB.Models;

namespace TianlalaHSB.ViewModels;

public class ReceiverViewModel : ObservableRecipient
{
    public ReceiverViewModel()
    {
        // 在构造函数中注册消息的接收者
        WeakReferenceMessenger.Default.Register<Message>(this, OnMyMessageReceived);
    }

    private void OnMyMessageReceived(object sender, Message message)
    {
        // 处理接收到的消息
        Console.WriteLine("Message received: " + message.Content);
    }

    ~ReceiverViewModel()
    {
        // 在析构函数中取消消息的注册
        WeakReferenceMessenger.Default.Unregister<Message>(this);
    }
}